<template>
<van-nav-bar
    title="收货地址"
    left-arrow
    @click-left="gotoleft"
  />
<div class="address-container">
  <div class="no-address" v-if="addressList.length === 0">
    <van-image class="address-img" :src="address"></van-image>
    <div class="tip">您还没有收货地址~</div>
    <router-link class="add-btn" :to="{name:'edit',params:{type:'add'}}" url="/pages/address/edit/index?type=add">添加收货地址</router-link>
  </div>
  <div class="address-list" v-else>
    <div class="address-item" v-for="item in addressList" :key="item.id">
      <div class="left">
        <div class="user">
          <text class="name">{{item.userName}}</text>
          <text class="phone">{{item.telNumber}}</text>
        </div>
        <div class="address">{{item.address}}</div>
      </div>
      <router-link class="edit" :to="{name:'edit',params:{id:item.id,type:'change'}}">
        <van-image class="edit-img" :src="edit"></van-image>
      </router-link>
        <van-image class="delete-img" :src="deleteimg"></van-image>
    </div>
  </div>
  <router-link :to="{name:'edit',params:{type:'add'}}">
    <van-button  type="primary" class="add">添加地址</van-button>
  </router-link>
</div>
</template>

<script>
import router from '../router'
import address from '../assets/images/address.png'
import edit from '../assets/images/edit.png'
import deleteimg from '../assets/images/delete.png'
import {
  GetAllAddress
} from '@/api'
import {reactive,toRefs} from 'vue'
export default {
  name: 'address',
  setup(props) {
    //参数
    let mine = reactive({
      addressList: [],
      address:address,
      edit:edit,
      deleteimg:deleteimg,
    })
    GetAllAddress().then(res => {
      console.log('get all address==>', res)
      if (res.code === 0) {
        let addressList = res.data;
        addressList.forEach(item => {
          item.address = `${item.provinceName}${item.cityName}${item.countyName}${item.detailInfo}`
        })
        mine.addressList = addressList
      }
    })
    function gotoleft(){
      router.go(-1)
    }
    //挂载
    return{ ...toRefs(mine),gotoleft}
  }
}
</script>

<style scoped>
page{
  width:100%;
  height: 100%;
}

.address-container{
  width:100%;
  height: 100%;
  padding-bottom: 100px;
  box-sizing: border-box;
  background-color: #eeeeee;
}

.no-address{
  display: flex;
  flex-direction: column;
  align-items: center;
  width:100%;
  height: 100%;
  padding-top: 100px;
  box-sizing: border-box;
}

.no-address .address-img{
  display: block;
  width:400px;
  height: 300px;
}

.no-address .tip{
  margin-top: 30px;
  color:#565656;
}

.no-address .add-btn{
  width:50%;
  height: 90px;
  margin-top: 80px;
  line-height: 90px;
  border-radius: 10px;
  text-align: center;
  color:#ffffff;
  background-color: #31b573;
  box-shadow: inset 0 -4px 5px 3px rgba(138, 202, 157,.8);  
}

.address-list{
  padding-bottom: 100px;
  box-sizing: border-box;
}

.address-item{
  display:flex;
  align-items: center;
  justify-content: space-between;
  padding:30px 50px;
  box-sizing: border-box;
  background-color: #ffffff;
}

.address-item .left{
  width:90%;
}

.address-item .left .address{
  max-width: 580px;
  word-break: break-all;
}

.phone{
  margin-left: 30px;
}

.address{
  margin-top: 20px;
}

.edit{
  display: flex;
  justify-content: center;
  align-items: center;
  width:10%;
}

.edit-img{
  display: block;
  width:40px;
  height: 40px;
}

.delete-img{
  display: block;
  width:60px;
  height: 60px;
  margin-left: 20px;
}

.add{
  position: fixed;
  bottom:20px;
  left: 50%;
  transform: translateX(-50%);
  width:300px;height:70px;border-radius:0;
}
</style>